<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>FLIP 动画</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    ul, li {
      list-style: none;
    }
    button {
      height: 35px;
      line-height: 35px;
      background-color: #04d1d1;
      margin: 10px;
    }
    .container_1 {
      margin: 10px;
      display: flex;
    }
    .container_1 li {
      width: 50px;
      height: 50px;
      text-align: center;
      line-height: 50px;
      margin-left: 10px;
    }
    .item_1{
      background-color: #f02e2e;
    }
    .item_2 {
      background-color: chocolate;
    }
    .item_3 {
      background-color: #e9ec05;
    }
    .item_4 {
      background-color: #099;
    }
    .item_5 {
      background-color: #10d4ee;
    }
  </style>
</head>
<body>
  <p>
    参考链接： <a href="https://cloud.tencent.com/developer/article/2383439">FLIP，一种高端优雅但简单易用的前端动画思维</a> 
  </p>
  <button class="shuffle_1">FLIP Shuffle</button>
  <ul class="container_1">
    <li class="item_1">01</li>
    <li class="item_2">02</li>
    <li class="item_3">03</li>
    <li class="item_4">04</li>
    <li class="item_5">05</li>
  </ul>
  <script>
    const container1 = document.querySelector('.container_1');
    const shuffle1 = document.querySelector('.shuffle_1');

    // Fist position
    function getLocation(el, direction) {
      const arr = [...el.children]
      for(let item of arr) {
        const rect = item.getBoundingClientRect();
        item.startPos = rect[direction]
        item.style.removeProperty('transition')
      }
      return arr;
    }

    // last position
    function change(arr, direction) {
      const len = arr.length;
      const parent = (arr[0]).parentNode;
     
      for(let i = 0; i < len; i++) {
        const nextPos = Math.floor(Math.random() * len)
 
        if (i != nextPos) {
          arr[i].aaa= [i, nextPos]
          const nextDoom = arr[i].nextElementSibling;
          // 两者位置对调 parent.insertBefore(newChild, existingChild: 现有节点)
          parent.insertBefore(arr[i], arr[nextPos]);  // 当前节点移动到 指定（nextPos）的节点之前
          parent.insertBefore(arr[nextPos], nextDoom);
        }
      }
   
    }
    function toInvert(arr){
      for(let item of arr) {
        const move = item.startPos - item.lastPos;
        item.style.transform = `translateX(${ move }px)`
      }
    }
    function play(container) {
      let arr = [...container.children];
      for(let item of arr) {
        const rect = item.getBoundingClientRect();
        item.style.transform = `translateX(${ item.startPos - rect.left }px)`

        ref(() => {
          // console.log(item.textContent)
          item.style.transition = 'transform 1s';
          item.style.transform = '';
         
        })
      }
    }

    function ref(callback) {
        requestAnimationFrame(() => {
          requestAnimationFrame(callback)
        })
      }
   
    shuffle1.onclick = () => {
      let arr = getLocation(container1, 'left');
      change(arr, 'left');
      play(container1)
    
    }

    function delay(duration = 1000) {
      const start = Date.now()
      while(Date.now() - start < duration) {}
    }
  </script>
</body>
</html>